using Newtonsoft.Json;
using System;

namespace MetaView.Core;

/// <summary>
/// Json字符串转到对应类
/// </summary>
public class JsonStringToClassSerializerFormatter<TState> : ISerializerFormatter<string, TState>
{
    /// <inheritdoc/>
    public int Order { get; set; }

    /// <summary>
    /// JsonSettings
    /// </summary>
    public JsonSerializerSettings JsonSettings { get; set; } = new JsonSerializerSettings();

    /// <inheritdoc/>
    /// <param name="state"></param>
    /// <param name="source"></param>
    /// <param name="targetType"></param>
    /// <param name="target"></param>
    /// <returns></returns>
    public virtual bool TryDeserialize(TState state, in string source, Type targetType, out object target)
    {
        try
        {
            target = JsonConvert.DeserializeObject(source, targetType, this.JsonSettings);
            return true;
        }
        catch
        {
            target = default;
            return false;
        }
    }

    /// <inheritdoc/>
    /// <param name="state"></param>
    /// <param name="target"></param>
    /// <param name="source"></param>
    /// <returns></returns>
    public virtual bool TrySerialize(TState state, in object target, out string source)
    {
        try
        {
            source = JsonConvert.SerializeObject(target, this.JsonSettings);
            return true;
        }
        catch
        {
            source = null;
            return false;
        }
    }
}